package com.se2.antiqueexhibition.config;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * MyBatis配置类
 * 解决MyBatis类型别名冲突问题
 */
@Configuration
@MapperScan("com.se2.antiqueexhibition.Mapper")
public class MyBatisConfig {

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        
        // 设置mapper文件位置
        sessionFactory.setMapperLocations(
            new PathMatchingResourcePatternResolver().getResources("classpath:com/se2/antiqueexhibition/Mapper/*.xml")
        );
        
        // 创建Configuration对象并设置相关属性
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setCallSettersOnNulls(true);
        configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
        
        // 手动注册类型别名，避免自动扫描导致的冲突
        configuration.getTypeAliasRegistry().registerAlias("user", "com.se2.antiqueexhibition.pojo.User");
        
        sessionFactory.setConfiguration(configuration);
        
        return sessionFactory.getObject();
    }
}